package action;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import bean.InsertUpdateDelBean;

public class FeijiServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public FeijiServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		response.setContentType("text/html;charset=gb2312");
		request.setCharacterEncoding("gb2312");
		
		String mark = request.getParameter("mark");
		String id = request.getParameter("id");
		String banci = request.getParameter("banci");
		String gongsi = request.getParameter("gongsi");
		String qifeichengshi = request.getParameter("qifeichengshi");
		String qifeijichang = request.getParameter("qifeijichang");
		String qifeitime = request.getParameter("qifeitime");
		String daodachengshi = request.getParameter("daodachengshi");
		String daodajichang = request.getParameter("daodajichang");
		String daodatime = request.getParameter("daodatime");
		String jingji = request.getParameter("jingji");
		String jingjisums = request.getParameter("jingjisums");
		String toudeng = request.getParameter("toudeng");
		String toudengsums = request.getParameter("toudengsums");
		String beizhu = request.getParameter("beizhu");
		String distance = request.getParameter("distance");
		String sql = "";
		String str = "";

		InsertUpdateDelBean ib = new InsertUpdateDelBean();
		
		if(mark != null && mark.equals("add")){
			sql = "insert into feiji(banci,gongsi,qifeichengshi,qifeijichang,qifeitime,daodachengshi,daodajichang,daodatime,jingji,jingjisums,toudeng,toudengsums,beizhu,dis) values('"+banci+"','"+gongsi+"','"+qifeichengshi+"','"+qifeijichang+"','"+qifeitime+"','"+daodachengshi+"','"+daodajichang+"','"+daodatime+"','"+jingji+"','"+jingjisums+"','"+toudeng+"','"+toudengsums+"','"+beizhu+"','"+toudengsums+"')";
			int flag = ib.insertANDupdateANDdel(sql);
			if(flag == -1){
				str = "/admin/feiji.jsp?mark="+mark;
				request.setAttribute("message", "航班重复！");
			}else{
				str = "/admin/feiji_list.jsp?banci="+banci;
				request.setAttribute("message", "操作成功!");
			}
		}
		if(mark != null && mark.equals("update")){
			sql = "update feiji set banci='"+banci+"',gongsi='"+gongsi+"',qifeichengshi='"+qifeichengshi+"',qifeijichang='"+qifeijichang+"',qifeitime='"+qifeitime+"',daodachengshi='"+daodachengshi+"',daodajichang='"+daodajichang+"',daodatime='"+daodatime+"',jingji='"+jingji+"',jingjisums='"+jingjisums+"',toudeng='"+toudeng+"',toudengsums='"+toudengsums+"',beizhu='"+beizhu+"' where id="+id;
			int flag = ib.insertANDupdateANDdel(sql);
			if(flag == -1){
				str = "/admin/feiji.jsp?mark="+mark+"&id="+id;
				request.setAttribute("message", "航班重复！");
			}else{
				str = "/admin/feiji_list.jsp?banci="+banci;
				request.setAttribute("message", "操作成功!");
			}
		}		

		request.getRequestDispatcher(str).forward(request, response);
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
